package me.leiusual.sharing.ui.mine

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations
import androidx.lifecycle.ViewModel
import me.leiusual.sharing.api.model.UserInfo
import me.leiusual.sharing.pref.User
import me.leiusual.sharing.repository.UserRepository
import me.leiusual.sharing.repository.model.Data
import me.leiusual.sharing.util.extension.toLiveData
import javax.inject.Inject

/**
 * Create by leiusual on 2018/7/28
 */
class MineNicknameViewModel @Inject constructor(
    private val userRepository: UserRepository,
    val user: User
) : ViewModel() {

  private val _nicknameUpdate = MutableLiveData<String>()
  val nicknameUpdate: LiveData<Data<Unit>> = Transformations.switchMap(_nicknameUpdate) {
    userRepository.nicknameUpdate(it).toLiveData()
  }

  fun nicknameUpdate(nickname: String) {
    _nicknameUpdate.value = nickname
  }

  private val _userInfo = MutableLiveData<Unit>()
  val userInfo: LiveData<Data<UserInfo.Response>> = Transformations.switchMap(_userInfo) {
    userRepository.userInfo().toLiveData()
  }

  fun userInfo() {
    _userInfo.value = Unit
  }
}